package com.ruoyi.web.controller.inventory;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.StoreBusiness;
import com.ruoyi.system.domain.WarehousingEntry;
import com.ruoyi.system.service.IStoreBusinessService;
import com.ruoyi.system.service.IWarehousingEntryService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.BaseResponse;
import com.ruoyi.system.util.StoreIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 入库单 Controller
 *
 * @author ruoyi
 * @date 2025-06-04
 */
@Api(tags = "门店入库单管理")
@RestController
@RequestMapping("/warehousing-entry")
public class WarehousingEntryController extends BaseController {

    @Autowired
    private IWarehousingEntryService warehousingEntryService;

    @Autowired
    private IStoreBusinessService storeBusinessService;

    @Autowired
    private StoreIdUtils storeIdUtils;

    /**
     * 分页查询入库单
     */
    @ApiOperation("分页查询入库单")
    @PostMapping("/page")
    public BaseResponse<IPage<WarehousingEntry>> page(@RequestBody WarehousingEntry warehousingEntry) {
        Page<WarehousingEntry> page = new Page<>(warehousingEntry.getPageNum(), warehousingEntry.getPageSize());
        LambdaQueryWrapper<WarehousingEntry> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!AbdulEmptyUtils.isEmpty(warehousingEntry.getWarehouseId()),WarehousingEntry::getWarehouseId, warehousingEntry.getWarehouseId());
        if (!AbdulEmptyUtils.isEmpty(warehousingEntry.getCreateTimeStart()) && !AbdulEmptyUtils.isEmpty(warehousingEntry.getCreateTimeEnd())){
            wrapper.between(WarehousingEntry::getCreateTime, warehousingEntry.getCreateTimeStart(), warehousingEntry.getCreateTimeEnd());
        }
        wrapper.eq(!AbdulEmptyUtils.isEmpty(warehousingEntry.getId()),WarehousingEntry::getId, warehousingEntry.getId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(warehousingEntry.getCode()),WarehousingEntry::getCode, warehousingEntry.getCode());
        wrapper.eq(WarehousingEntry::getStoreId, warehousingEntry.getStoreId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(warehousingEntry.getIsType()),WarehousingEntry::getIsType, warehousingEntry.getIsType());
        wrapper.eq(WarehousingEntry::getStoreId, storeIdUtils.getStoreId());
        wrapper.orderByDesc(WarehousingEntry::getUpdateTime);
        IPage<WarehousingEntry> result = warehousingEntryService.page(page);
        if (!AbdulEmptyUtils.isEmpty(result.getRecords())){
            Set<Long> storeBusinessIds = result.getRecords().stream().map(WarehousingEntry::getStoreBusinessId).collect(Collectors.toSet());
            if (!AbdulEmptyUtils.isEmpty(storeBusinessIds)){
                LambdaQueryWrapper<StoreBusiness> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.in(StoreBusiness::getId, storeBusinessIds);
                List<StoreBusiness> list = storeBusinessService.list(lambdaQueryWrapper);
                if (!AbdulEmptyUtils.isEmpty(list)){
                    Map<Long, String> map = list.stream().collect(Collectors.toMap(StoreBusiness::getId, StoreBusiness::getName));
                    for (WarehousingEntry record : result.getRecords()) {
                        record.setStoreBusinessName(map.get(record.getStoreBusinessId()));
                    }
                }
            }
        }
        return BaseResponse.success(result);
    }

    /**
     * 新增入库单
     */
    @ApiOperation("新增入库")
    @PostMapping("/save")
    public BaseResponse<Boolean> save(@RequestBody WarehousingEntry warehousingEntry) {
        boolean result = warehousingEntryService.add(warehousingEntry);
        return BaseResponse.success(result);
    }

    /**
     * 商品出库
     */
    @ApiOperation("商品出库")
    @PostMapping("/out")
    public BaseResponse<Boolean> out(@RequestBody WarehousingEntry warehousingEntry) {
        boolean result = warehousingEntryService.out(warehousingEntry);
        return BaseResponse.success(result);
    }

    /**
     * 根据ID查询详情
     */
    @ApiOperation("根据ID查询入库单详情")
    @GetMapping("/detail/{id}")
    public BaseResponse<WarehousingEntry> detail(@PathVariable Long id) {
        WarehousingEntry result = warehousingEntryService.getById(id);
        return BaseResponse.success(result);
    }
}
